﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal abstract class MySQLDAO : IDisposable
    {
        #region Fields

        private MySqlConnection dbConnection;
        private bool isInitialized;
        private bool isDisposed;

        #endregion

        #region Properties

        /// <summary>
        /// Get the MySQL DB connection object.
        /// </summary>
        public MySqlConnection DB
        {
            get { return dbConnection; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new MySQLDAO object.
        /// </summary>
        public MySQLDAO()
        {
            dbConnection = null;
            isDisposed = false;
            isInitialized = false;
        }

        /// <summary>
        /// Ensure the DB Connection is properly disposed of.
        /// </summary>
        ~MySQLDAO()
        {
            Dispose();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Initialize this repository.
        /// </summary>
        public void Initialize()
        {
            MySqlConnectionStringBuilder mySQLConnStringBuilder = new MySqlConnectionStringBuilder();
            mySQLConnStringBuilder.Server = Properties.Settings.Default.MySQLDatabaseServer;
            mySQLConnStringBuilder.Database = "ros_mysql_main";
            mySQLConnStringBuilder.UserID = Properties.Settings.Default.MySQLDatabaseUserName;
            mySQLConnStringBuilder.Password = Properties.Settings.Default.MySQLDatabasePassword;
            dbConnection = new MySqlConnection(mySQLConnStringBuilder.GetConnectionString(true));
            dbConnection.Open();
            isInitialized = true;
        }

        /// <summary>
        /// Dispose of this repository.
        /// </summary>
        public void Dispose()
        {
            if (!this.isDisposed)
            {
                dbConnection.Dispose();
                isDisposed = true;
                isInitialized = false;
            }
        }            

        /// <summary>
        /// Ensure that the DAO has been initialized/not disposed.
        /// </summary>
        private void checkState()
        {
            if (!isInitialized || isDisposed)
                throw new InvalidOperationException();
        }

        #endregion
    }
}
